package nullby.yoga.utils;

import java.util.Locale;

import nullby.yoga.R;
import nullby.yoga.activity.MainActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.SharedPreferences;
import android.widget.Toast;

public class PrefManager {

	public static final String DIR_NAME = "EuropeanYoga";
	public static final String KEY_LANG = "lang";
	public static final String KEY_SEX = "sex";
	public static final String KEY_SEX_PREF = "sex_";
	public static final String KEY_SONG = "song";
	public static final String KEY_SONG_PREF = "song_";

	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;

	private Context mContext;

	private static PrefManager instance;

	private PrefManager(Context context) {
		if (instance == null) {
			mContext = context;
			preferences = mContext.getSharedPreferences(Extra.PREFERENCES,
					mContext.MODE_PRIVATE);
			editor = preferences.edit();
			init();
		}
	}

	public static PrefManager get() {
		return instance;
	}

	public static PrefManager newInstance(Context context) {
		if (instance == null) {
			instance = new PrefManager(context);
		}
		return instance;
	}

	public void commit() {
		editor.commit();
	}

	public SharedPreferences.Editor getEditor() {
		return editor;
	}

	public void init() {
		if ("".equals(preferences.getString(Extra.LANG, ""))
				&& "".equals(preferences.getString(Extra.SEX, ""))
				&& "".equals(preferences.getString(Extra.SONG, ""))) {
			setButtonSound(true);
			String defLang = "en";
			if ("en".equals(Locale.getDefault().getLanguage())) {
				defLang = "en";
			} else if ("cn".equals(Locale.getDefault().getLanguage())) {
				defLang = "cn";
			} else {
				defLang = "en";
			}
			putLang(defLang);
			putSex(null, Extra.GIRL);
			FileManager.copyFileFromRawIntoSd(mContext, R.raw.song,
					Extra.DEF_SONG_FILE_NAME);

			if (FileManager.isExists(Extra.DEF_SONG_FILE_NAME)
					&& FileManager.getFileSize(Extra.DEF_SONG_FILE_NAME) != 0L) {
				putSong(null, Extra.DEF_SONG_FILE_NAME);
			} else {
				// Toast.makeText(mContext, "ERROR INIT", 0).show();
			}
		}
	}

	public void setPic() {
		if ("".equals(preferences.getString("pic", ""))
				|| "man".equals(preferences.getString("pic", ""))) {
			getEditor().putString("pic", "women");
		} else {
			getEditor().putString("pic", "man");
		}
		editor.commit();
	}

	public String getPic() {
		return preferences.getString("pic", "women");
	}

	public void setButtonSound(final Boolean isButtonSound) {
		getEditor().putBoolean(Extra.BUTTON_SOUND, isButtonSound);
		editor.commit();
	}

	public Boolean isButtonSound() {
		return preferences.getBoolean(Extra.BUTTON_SOUND, false);
	}

	public void putLang(final String lang) {
		getEditor().putString(KEY_LANG, lang);
		editor.commit();
	}

	public String getLang() {
		return preferences.getString(KEY_LANG, "");
	}

	public void putSex(final String id, final String sex) {
		if (id == null) {
			getEditor().putString(KEY_SEX, sex);
		} else {
			getEditor().putString(KEY_SEX_PREF.concat(id), sex);
		}
		commit();
	}

	public String getSex(final String id) {
		if (id == null) {
			return preferences.getString(KEY_SEX, "");
		} else {
			return preferences.getString(KEY_SEX_PREF.concat(id), "");
		}
	}

	public void putSong(final String id, final String song) {
		if (id == null) {
			getEditor().putString(KEY_SONG, song);
		} else {
			getEditor().putString(KEY_SONG_PREF.concat(id), song);
		}
		commit();
	}

	public String getSong(final String id) {
		if (id == null) {
			return preferences.getString(KEY_SONG, "");
		} else {
			return preferences.getString(KEY_SONG_PREF.concat(id), "");
		}
	}

}
